#include "main.h"

int client(int argc, char **argv) {
	char sockname[500]=APPLICATION_SOCKET;
	register int s;
	int t;
	struct sockaddr_un sa;
	char buf[10001];
	long len;

	if(argc!=2)
		fatal("argc should be equal 2");

	if ((s = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1)
		fatal("socket()");
		
	bzero(&sa, sizeof sa);
	trace("Trying to connect...");
	sa.sun_family = AF_UNIX;
	strcpy(sa.sun_path, sockname);
	if (connect(s, (struct sockaddr *)&sa, sizeof sa) < 0) {
		perror("connect()");
		close(s);
		return 2;
	}
	
	trace("Reading file %s", argv[1]);
	FILE *f=fopen(argv[1], "r");
	while(fgets(buf, 10000, f)){
		if(buf[0]=='#')
			continue;
		send(s, buf, strlen(buf), 0);
		//write(s, buf, sizeof(buf));
	}
	send(s, "\n###\n", 5, 0);
	while(len=read(s, buf, 10000)){
		buf[len]=0;
		printf("%s", buf);
	}
		
	fclose(f);
	close(s);
	exit(0);
}

